<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>更新表头</title>
  <script src="./js/jexcel.js"></script>
  <script src="./js/jsuites.js"></script>
  <link rel="stylesheet" href="./css/jexcel.css" type="text/css" />
  <link rel="stylesheet" href="./css/jsuites.css" type="text/css" />
</head>

<body>
  <h1>更新表头</h1>
  <ul>
    <li>用户单击所选标题并按住鼠标 2 秒，提示将请求新标题；</li>
    <li>通过快捷菜单。用户在列中右键单击并选择选项Rename列；</li>
    <li>使用方法setHeader(colNumber, title)；</li>
  </ul>
  <div id="spreadsheet"></div>

  <div>
    <select id='columnNumber'>
      <option value="0">第0列</option>
      <option value="1">第1列</option>
      <option value="2">第2列</option>
    </select>
    <input id="btn1" type='button' value='设置表头标题' />
    <input id="btn2" type='button' value='获取表头标题' />
  </div>
</body>
<script type="module">
  import { text } from "./js/language.js";
  /*
    有三种方法可以更改表格标题：
    1、用户单击所选标题并按住鼠标 2 秒，提示将请求新标题；
    2、通过快捷菜单。用户在列中右键单击并选择选项Rename列；
    3、使用方法setHeader(colNumber, title)；
   */

  let oBtn1 = document.getElementById('btn1');
  let oBtn2 = document.getElementById('btn2');

  var data = [
    ['BR', 'Cheese', 1],
    ['CA', 'Apples', 0],
    ['US', 'Carrots', 1],
    ['GB', 'Oranges', 0],
  ];

  let table = jspreadsheet(document.getElementById('spreadsheet'), {
    data: data,
    columns: [
      {
        type: 'autocomplete',
        title: 'Country',
        width: '300',
        url: './static/countries.json'
      },
      {
        type: 'dropdown',
        title: 'Food',
        width: '150',
        source: ['Apples', 'Bananas', 'Carrots', 'Oranges', 'Cheese']
      },
      {
        type: 'checkbox',
        title: 'Stock',
        width: '100'
      },
    ],
    text
  });

  // 设置表头标题
  oBtn1.onclick = function () {
    table.setHeader(document.getElementById('columnNumber').value)
  }

  // 获取表头标题
  oBtn2.onclick = function () {
    let val = table.getHeader(document.getElementById('columnNumber').value)
    alert(val)
  }
</script>

</html>